/* $Header: pneu_diag.c,v 2.2 01/10/02 10:26:10 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  pneu_diag.c                                           */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To define the pneumatics diagnostic routines.      */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <p_status.h>
#include <status.h>
#include <diag.h>
#include <p_ui.h>

#include <sig_types.h>
#include <sig_ui.h>

#include <p_diag.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

UI_ERR SetInletPresDCOnOff( pINLET_STATUS pinlet, ON_OFF on_off )
{
   if ( on_off == OFF )
   {
      return SetInletPresDutyCycleSetpt( pinlet, 0 );
   }

   return SetInletPresDC( pinlet, 1 );
}

/* Provide a wrapper function to set GC in diagnostics mode if needed */
UI_ERR SetInletPresDC( pINLET_STATUS pinlet, INT32 duty_cycle )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   return SetInletPresDutyCycleSetpt( pinlet, duty_cycle );
}

UI_ERR SetInletFlowDCOnOff( pINLET_STATUS pinlet, ON_OFF on_off )
{
   if ( on_off == OFF )
   {
      return SetInletFlowDutyCycleSetpt( pinlet, 0 );
   }
   return SetInletFlowDC( pinlet, 1 );
}

/* Provide a wrapper function to set GC in diagnostics mode if needed */
UI_ERR SetInletFlowDC( pINLET_STATUS pinlet, INT32 duty_cycle )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   return SetInletFlowDutyCycleSetpt( pinlet, duty_cycle );
}

UI_ERR SetDetPresDCOnOff( pDETECTOR_STATUS pdet, U_INT8 gas_num, ON_OFF on_off )
{
   if ( on_off == OFF )
   {
      return SetDetPresDutyCycleSetpt( pdet, gas_num, 0 );
   }

   return SetDetPresDC( pdet, gas_num, 1 );
}

/* Provide a wrapper function to set GC in diagnostics mode if needed */
UI_ERR SetDetPresDC( pDETECTOR_STATUS pdet, U_INT8 gas_num, INT32 duty_cycle )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   return SetDetPresDutyCycleSetpt( pdet, gas_num, duty_cycle );
}

UI_ERR SetAuxPresDCOnOff( pAUX_STATUS paux, ON_OFF on_off )
{
   if ( on_off == OFF )
   {
      return SetAuxPresDutyCycleSetpt( paux, 0 );
   }

   return SetAuxPresDC( paux, 1 );
}

/* Provide a wrapper function to set GC in diagnostics mode if needed */
UI_ERR SetAuxPresDC( pAUX_STATUS paux, INT32 duty_cycle )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   return SetAuxPresDutyCycleSetpt( paux, duty_cycle );
}

/* Used for data comm */
UI_ERR SetDiagPneuDutyCycle( U_INT8 channel, INT32 duty_cycle )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   return SetDutyCycleSetpt( channel, duty_cycle );
}

void ClearDiagPneuDutyCycle( U_INT8 channel )
{
    if ( GetDutyCycleSetpt( channel ) != 0 ) (void) SetDutyCycleSetpt( channel, 0 );
}

/***********************************************************************/
/* FUNCTION:  SetSplitValveManualControl                               */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To enable/disable the manual control of the               */
/*           split/splitless valve.                                    */
/*                                                                     */
/* RETURNED VALUE:  UI_ERR return code                                 */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  SetDiagMode( ON )                                           */
/*                                                                     */
/* LIMITATIONS:  This will not clear the diag mode.                    */
/*                                                                     */
/***********************************************************************/

UI_ERR SetSplitValveManualControl( pINLET_STATUS status, ON_OFF on_off )
{
   UI_ERR error;

   if ( on_off == ON )
   {
      error = SetDiagMode( ON );

      if ( error != OK )
      {
         return error;
      }
   }

   SetInletDiagMode( status, (BIT8)on_off );
   return OK;
}

pINLET_STATUS PositionToInlet( POSITION position )
{
   if ( position == FRONT ) return pFRONT_INLET;

   return pBACK_INLET;
}

INT32 GetPresDiagValueFromSignal( INT32 signal )
{
   /* Clamp at zero, pressure and flow should not be negative */
   if ( signal < 0 ) signal = 0;

   return ( (signal*100)/240 + 5 )/10;
}

static INT32 GetFlowDiagValueFromSignal( INT32 signal )
{
   /* Clamp at zero, pressure and flow should not be negative */
   if ( signal < 0 ) signal = 0;

   return ( (signal*1000)/240 + 5 )/10;
}

/*********************/
/* Inlet diag values */
/*********************/

#define ACTUAL_SIGNAL 0
#define SETPT_SIGNAL  1

const SIG_TYPE INLET_PRES_DIAG_SIGNALS[2][2]=
{
   F_INLET_PRES_SIGNAL, F_INLET_PRES_SETPT_SIGNAL,
   B_INLET_PRES_SIGNAL, B_INLET_PRES_SETPT_SIGNAL
};

const SIG_TYPE INLET_FLOW_DIAG_SIGNALS[2][2]=
{
   F_INLET_FLOW_SIGNAL, F_INLET_FLOW_SETPT_SIGNAL,
   B_INLET_FLOW_SIGNAL, B_INLET_FLOW_SETPT_SIGNAL
};

static INT32 GetInletPresDiagValue( BIT8 position, BIT8 setpt )
{
   SIG_TYPE type;

   type = INLET_PRES_DIAG_SIGNALS[position][setpt];

   return GetPresDiagValueFromSignal( GetPresSignalValue( type ) );
}

static INT32 GetInletFlowDiagValue( BIT8 position, BIT8 setpt )
{
   SIG_TYPE type;

   type = INLET_FLOW_DIAG_SIGNALS[position][setpt];

   return GetFlowDiagValueFromSignal( GetAlternateSignalValue( type ) );
}

INT32 GetInletPresDiagSetpt( BIT8 position )
{
   return GetInletPresDiagValue( position, SETPT_SIGNAL );
}

INT32 GetInletPresDiagActual( BIT8 position )
{
   return GetInletPresDiagValue( position, ACTUAL_SIGNAL );
}

INT32 GetInletFlowDiagSetpt( BIT8 position )
{
   return GetInletFlowDiagValue( position, SETPT_SIGNAL );
}

INT32 GetInletFlowDiagActual( BIT8 position )
{
   return GetInletFlowDiagValue( position, ACTUAL_SIGNAL );
}



/*******************/
/* Det diag values */
/*******************/

const SIG_TYPE F_DET_PRES_DIAG_SIGNALS[3][2]=
{
   F_DET_GAS1_PRES_SIGNAL, F_D_GAS1_P_SETPT_SIGNAL,
   F_DET_GAS2_PRES_SIGNAL, F_D_GAS2_P_SETPT_SIGNAL,
   F_DET_GAS3_PRES_SIGNAL, F_D_GAS3_P_SETPT_SIGNAL
};

const SIG_TYPE B_DET_PRES_DIAG_SIGNALS[3][2]=
{
   B_DET_GAS1_PRES_SIGNAL, B_D_GAS1_P_SETPT_SIGNAL,
   B_DET_GAS2_PRES_SIGNAL, B_D_GAS2_P_SETPT_SIGNAL,
   B_DET_GAS3_PRES_SIGNAL, B_D_GAS3_P_SETPT_SIGNAL
};

const SIG_TYPE F_DET_FLOW_DIAG_SIGNALS[3][2]=
{
   F_DET_GAS1_FLOW_SIGNAL, F_D_GAS1_F_SETPT_SIGNAL,
   F_DET_GAS2_FLOW_SIGNAL, F_D_GAS2_F_SETPT_SIGNAL,
   F_DET_GAS3_FLOW_SIGNAL, F_D_GAS3_F_SETPT_SIGNAL
};

const SIG_TYPE B_DET_FLOW_DIAG_SIGNALS[3][2]=
{
   B_DET_GAS1_FLOW_SIGNAL, B_D_GAS1_F_SETPT_SIGNAL,
   B_DET_GAS2_FLOW_SIGNAL, B_D_GAS2_F_SETPT_SIGNAL,
   B_DET_GAS3_FLOW_SIGNAL, B_D_GAS3_F_SETPT_SIGNAL
};

static INT32 GetDetPresDiagValue( POSITION position, U_INT8 gas_num, BIT8 setpt )
{
   SIG_TYPE type;

   if ( position == FRONT )
   {
      type = F_DET_PRES_DIAG_SIGNALS[gas_num-1][setpt];
   }
   else
   {
      type = B_DET_PRES_DIAG_SIGNALS[gas_num-1][setpt];
   }

   return GetPresDiagValueFromSignal( GetPresSignalValue( type ) );
}

static INT32 GetDetFlowDiagValue( POSITION position, U_INT8 gas_num, BIT8 setpt )
{
   SIG_TYPE type;

   if ( position == FRONT )
   {
      type = F_DET_FLOW_DIAG_SIGNALS[gas_num-1][setpt];
   }
   else
   {
      type = B_DET_FLOW_DIAG_SIGNALS[gas_num-1][setpt];
   }

   return GetFlowDiagValueFromSignal( GetAlternateSignalValue( type ) );
}

INT32 GetDetPresDiagSetpt( BIT8 position, U_INT8 gas_num )
{
   return GetDetPresDiagValue( (POSITION)position, gas_num, SETPT_SIGNAL );
}

INT32 GetDetPresDiagActual( BIT8 position, U_INT8 gas_num )
{
   return GetDetPresDiagValue( (POSITION)position, gas_num, ACTUAL_SIGNAL );
}

INT32 GetDetFlowDiagSetpt( BIT8 position, U_INT8 gas_num )
{
   return GetDetFlowDiagValue( (POSITION)position, gas_num, SETPT_SIGNAL );
}

INT32 GetDetFlowDiagActual( BIT8 position, U_INT8 gas_num )
{
   return GetDetFlowDiagValue( (POSITION)position, gas_num, ACTUAL_SIGNAL );
}


/*******************/
/* Aux diag values */
/*******************/

const SIG_TYPE AUX_DIAG_SIGNALS[3][2]=
{
   AUX_3_PRES_SIGNAL, AUX_3_PRES_SETPT_SIGNAL,
   AUX_4_PRES_SIGNAL, AUX_4_PRES_SETPT_SIGNAL,
   AUX_5_PRES_SIGNAL, AUX_5_PRES_SETPT_SIGNAL

};

static INT32 GetAuxPresDiagValue( BIT8 aux_number, BIT8 setpt )
{
   SIG_TYPE type;

   type = AUX_DIAG_SIGNALS[aux_number-3][setpt];

   return GetPresDiagValueFromSignal( GetPresSignalValue( type ) );
}

INT32 GetAuxPresDiagSetpt( BIT8 aux_number )
{
   return GetAuxPresDiagValue( aux_number, SETPT_SIGNAL );
}

INT32 GetAuxPresDiagActual( BIT8 aux_number )
{
   return GetAuxPresDiagValue( aux_number, ACTUAL_SIGNAL );
}

BIT8 InletHasFlowSensor( pINLET_STATUS pinlet )
{
   switch ( GetInletType(pinlet) )
   {
      case PP:
      case SS:
      case G_PTV:
      case CIS3:
      case CIS4:
      case ACI:
      case SIMDIST:
      case VOLATILES:
      case JIB:  return TRUE;
      default:   return FALSE;
   }
}
